﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Hardcodet.Wpf.TaskbarNotification;
using VirtualKeyboard.UI.Localize;

namespace VirtualKeyboard.UI
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainView
    {
        //private ConfigView configView;

        public MainView()
        {
            InitializeComponent();
            AddHandler(LoadedEvent, new RoutedEventHandler(OnLoad));
        }

        private void OnLoad(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
            ShowInTaskbar = false;

            var title = AppResources.StartMessage;
            var text =  AppResources.LocalIP + GetLocalIP();

            myNotifyIcon.ShowBalloonTip(title, text, BalloonIcon.None);
            var dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += OnDispatcherTimerTick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 3);
            dispatcherTimer.Start();
        }

        private string GetLocalIP()
        {
            var ipstr = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault(ip => ip.AddressFamily != AddressFamily.InterNetworkV6);
            return ipstr != null ? ipstr.ToString() : "";
        }

        private void OnDispatcherTimerTick(object sender, EventArgs e)
        {
            myNotifyIcon.HideBalloonTip();
        }
        
        private void OnExitMenuClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

    }
}
